<template>
    <div style="height:100%;">
        <vue-better-scroll class="wrapper"
                           ref="scroll"
                           :scrollbar="scrollbarObj"
                           :pullDownRefresh="pullDownRefreshObj"
                           :pullUpLoad="pullUpLoadObj"
                           :startY="parseInt(startY)"
                           @pullingUp="onPullingUp">
            <div class="form-section">
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>单位名称</label>
                    </div>
                    <div class="equipment-right-select"
                         @click="selectType">
                        <span>{{checkcompany.Name?checkcompany.Name:'请选择'}}</span>
                        <i class="iconfont icon-jinru rightmsg"></i>
                    </div>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>统计指标</label>
                    </div>
                    <group>
                        <popup-picker :data="accopt"
                                      placeholder='请选择'
                                      v-model="accoptVal"></popup-picker>
                    </group>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>开始时间</label>
                    </div>
                    <group>
                        <datetime v-model="startTime"></datetime>
                    </group>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>结束时间</label>
                    </div>
                    <group>
                        <datetime v-model="endTime"></datetime>
                    </group>
                </div>
                <div class="btns">
                    <!-- <button>保存</button> -->
                    <button class="green width200"
                            @click="getData">查询</button>
                </div>
            </div>
            <h3 v-if="SIndicators == '设备类型'">设备台数：{{allNumDetail.Total}}台 塔式起重机：{{allNumDetail.A}}台，施工升降机：{{allNumDetail.B}}台，物料提升机：{{allNumDetail.D}}台，桥（门）式起重机：{{allNumDetail.c}}台，其他：{{allNumDetail.E}}台</h3>
            <h3 v-if="SIndicators == '是否在场'">设备台数：{{allNumDetail.Total}}台 在场设备：{{allNumDetail['是']}}台，不在场设备：{{allNumDetail['否']}}台</h3>
            <h3 v-if="SIndicators == '是否自有设备'">设备台数：{{allNumDetail.Total}}台 自有设备：{{allNumDetail['是']}}台，非自有设备：{{allNumDetail['否']}}台</h3>
            <h3 v-if="SIndicators == '检测率'">设备台数：{{allNumDetail.Total}}台 已检测：{{allNumDetail.Check}}台，未检测：{{allNumDetail.Uncheck}}台，检测率：{{allNumDetail.CheckPercege}}</h3>
            <ul class="list">
                <li v-for="(item,index) in lists"
                    :key="index">
                    <h4>{{item.DepartName}}</h4>
                    <p v-if="SIndicators == '设备类型'">(塔式起重机：{{item.A}}台，施工升降机：{{item.B}}台，物料提升机：{{item.D}}台，桥（门）式起重机：{{item.c}}台，其他：{{item.E}}台，总数：{{item.Total}}台)</p>
                    <p v-else-if="SIndicators == '是否在场'">(设备台数：{{item.Total}}台，在场设备：{{item['是']}}台，不在场设备：{{item['否']}}台)</p>
                    <p v-else-if="SIndicators == '是否自有设备'">(设备台数：{{item.Total}}台，自有设备：{{item['是']}}台，非自有设备：{{item['否']}}台)</p>
                    <p v-else-if="SIndicators == '检测率'">(设备台数：{{item.Total}}台，已检测：{{item.Check}}台，未检测：{{item.Uncheck}}台，检测率：{{item.CheckPercege}})</p>
                </li>
            </ul>
        </vue-better-scroll>
        <myMenu></myMenu>
        <companyProp :companyPropShow="companyPropShow"
                     :selectCompany="selectCompany"></companyProp>
    </div>
</template>
<script>
import VueBetterScroll from 'vue2-better-scroll'
import formatDate from '@/utils/formatDate'
import myMenu from '@/commonComponents/menu'
import companyProp from '@/commonComponents/companyProp'
export default {
    name: 'statisticAnalysis',
    data () {
        return {
            scrollbarObj: {
                fade: true
            },
            // 这个配置用于做下拉刷新功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启下拉刷新，可以配置顶部下拉的距离（threshold） 来决定刷新时机以及回弹停留的距离（stop）
            pullDownRefreshObj: false,
            // 这个配置用于做上拉加载功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启上拉加载，可以配置离底部距离阈值（threshold）来决定开始加载的时机
            pullUpLoadObj: {
                threshold: 0,
                txt: {
                    more: '加载更多',
                    noMore: '没有更多数据了'
                }
            },
            startY: 0, // 纵轴方向初始化位置
            accopt: [['设备类型', '是否在场', '是否自有设备', '检测率']], // 验收结果
            accoptVal: ['设备类型'],
            startTime: `${formatDate().split('-')[0]}-${formatDate().split('-')[1]}-01`,
            endTime: formatDate(),
            userInfo: {},
            checkcompany: {
            }, //  单位名称
            SIndicators: '设备类型', // 统计指标
            lists: [],
            allNumDetail: {

            },
            pageIndex: 0, // 当前页
            listAllCount: 0, // 总条数
            companyPropShow: false
        }
    },
    components: {
        VueBetterScroll,
        myMenu,
        companyProp
    },
    created () {
        this.userInfo = JSON.parse(localStorage.getItem('userInfo')) || {}
        this.checkcompany = {
            Name: this.userInfo.deptname,
            Id: this.userInfo.deptid
        }
        this.getList()
    },
    methods: {
        selectType () {
            // this.$router.push({ path: '/testing/lookcompany' })
            this.companyPropShow = true
        },
        getList () {
            this.$vux.loading.show({
                text: '加载中...'
            })
            this.SIndicators = this.accoptVal[0]
            let params = {
                'allowPaging': false,
                'business': 'getSpecialEquipmentStatictis',
                'data': {
                    Category: '0',
                    DepartID: this.checkcompany.Id,
                    categoryName: this.SIndicators,
                    StartDate: this.startTime,
                    EndDate: this.endTime,
                    projectName: this.checkcompany.Name
                },
                'pageIndex': this.pageIndex,
                'pageSize': 20,
                'tokenId': '',
                'userId': this.userInfo.userid
            }
            switch (this.SIndicators) {
                case '设备类型':
                    params.data.Category = '0'
                    break
                case '是否在场':
                    params.data.Category = '1'
                    break
                case '是否自有设备':
                    params.data.Category = '2'
                    break
                case '检测率':
                    params.data.Category = '3'
                    break
                default:
                    params.data.Category = '0'
                    break
            }
            this.$axiosAjax.getConfigJson(params).then((res) => {
                if (res.data.code === 0) {
                    this.listAllCount = res.data.count
                    this.lists = this.lists.concat(res.data.data)
                    if (this.lists.length < this.listAllCount) {
                        this.$refs.scroll.forceUpdate(true)
                    } else {
                        this.$refs.scroll.forceUpdate(false)
                    }
                    this.allNumDetail = {}
                    this.lists.forEach(item => {
                        for (const key in item) {
                            if (key != 'DepartID' && key != 'DepartName') {
                                this.allNumDetail[key] = this.allNumDetail[key] ? this.allNumDetail[key] : 0
                                this.allNumDetail[key] += parseInt(item[key])
                            }
                        }
                    })
                    this.$forceUpdate()
                }
            }).catch(() => {

            }).finally(() => {
                this.$vux.loading.hide()
            })
        },
        onPullingUp () {
            this.pageIndex++
            this.getList()
        },
        getData () {
            this.pageIndex = 0
            this.lists = []
            this.getList()
        },
        selectCompany (item) {
            this.companyPropShow = false
            this.checkcompany = {
                Name: item.Name,
                Id: item.Id
            }
        }
    }
}
</script>
<style lang="less" scoped>
h3 {
    height: auto;
    line-height: 2em;
    color: #999;
    font-size: 12px;
    background: #ebeef2;
    text-align: left;
    padding: 10px 20px;
}
.list {
    padding-left: 20px;
    li {
        padding: 12px 0;
        border-bottom: 1px solid #efeff4;
    }
    h4 {
        line-height: 32px;
        font-size: 14px;
        color: #333;
    }
    p {
        line-height: 32px;
        font-size: 14px;
        color: #999;
        padding-right: 10px;
    }
}
</style>
